<?php
set_time_limit(600);
if (!$_SESSION['memberID']) {
    exit('{"status":"fail","msg":"unlogin"}');
}
$text = trim($_POST['text']);
if (empty($text)) {
    exit('{"status":"fail","msg":"empty text"}');
}
$AccessTokenUri = "https://oxford-speech.cloudapp.net:443/token/issueToken";

// Note: Sign up at http://www.projectoxford.ai to get a subscription key.  
// Search for Speech APIs from Azure Marketplace.  
// Use the subscription key as Client secret below.
$clientId     = "pft12301";
$clientSecret = "9c283cc7ffad4a24871fa6fc668f7d55";
$ttsHost      = "https://speech.platform.bing.com";
$data         = array(
    'grant_type'    => 'client_credentials',
    'client_id'     => $clientId,
    'client_secret' => $clientSecret,
    'scope'         => $ttsHost
);
$data = http_build_query($data);
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n" .
        "content-length: ".strlen($data)."\r\n",
        'method'  => 'POST',
        'content' => $data,
    ),
);
$context  = stream_context_create($options);
//get the Oxford Access Token in json
$result = file_get_contents($AccessTokenUri, false, $context);

if (!$result) {
    throw new Exception("Problem with $AccessTokenUri, $php_errormsg");
  }
else{
//   echo "Oxford Access Token: ". $result. "\n";
  
   // decode the json to get the Oxford Access Token object.
   $OxfordAcessToken = json_decode($result);
   $access_token = $OxfordAcessToken->{'access_token'};

   $ttsServiceUri = "https://speech.platform.bing.com:443/synthesize";

   $data = <<<XML
<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="http://www.w3.org/2001/mstts" xml:lang="zh-CN">
<voice name="Microsoft Server Speech Text to Speech Voice (zh-CN, Yaoyao, Apollo)">
{$text}
</voice>
</speak>
XML;
//   echo "tts post data: ". $data . "\n";
   $options = array(
    'http' => array(
        'header'  => "Content-type: application/ssml+xml\r\n" .
                    "X-Microsoft-OutputFormat: riff-16khz-16bit-mono-pcm\r\n" .
                    "Authorization: "."Bearer ".$access_token."\r\n" .
                    "X-Search-AppId: 07D3234E49CE426DAA29772419F436CA\r\n" .
                    "X-Search-ClientID: 1ECFAE91408841A480F00935DC390960\r\n" .
                    "User-Agent: TTSPHP\r\n" .
                    "content-length: ".strlen($data)."\r\n",
        'method'  => 'POST',
        'content' => $data,
        ),
    );
    $context  = stream_context_create($options);
    // get the wave data
    $result = file_get_contents($ttsServiceUri, false, $context);
    if (!$result) {
        echo '{"status":"fail","url":"","msg":"'.$php_errormsg.'"}';
//        throw new Exception("Problem with $AccessTokenUri, $php_errormsg");
      }
    else{
        $file_name  = "{$_SERVER['REQUEST_TIME']}.wav";
        $fp         = fopen("/databak/images/audio/$file_name", "w");
        fwrite($fp, $result);
        fclose($fp);
        echo '{"status":"ok","url":"http://images.12301.cc/audio/'.$file_name.'"}';
    }
}

